/**
 @Name：角色管理
 @Author：cike
 @Date：2019-5-26
**/

layui.define(['table', 'form','treeGrid'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,setter = layui.setter
  ,form = layui.form
  ,treeGrid = layui.treeGrid;

  // table设置
  table.set({
    where: {
      access_token: layui.data(setter.tableName)[setter.request.tokenName]
    }
    ,method: 'post'
    ,response:{
      statusCode: 200
    }
    ,request:{
      pageName: "page"
      ,limitName: "size"
    }
    ,page: true
    ,text: { none : "暂无相关数据" }
    ,done: function (res) {
      if(res[setter.response.statusCode] == setter.response.statusCode.logout){
        view.exit();
      }
    }
    ,parseData: function(res){ //res 即为原始返回的数据
      return {
        "code": res.code, //解析接口状态
        "msg": res.msg, //解析提示文本
        "count": res.data.total, //解析数据长度
        "data": res.data.list //解析数据列表
      };
    }
  });
  
  //角色管理
  table.render({
    elem: '#back-role-list'
    ,url: setter.api_back_role_list
    ,cols: [[
      {field: 'id', width: 65, title: 'ID',sort: true}
      ,{field: 'sort', width: 65, title: '排序', sort: true,edit:'text'}
      ,{field: 'name', width: 100, title: '角色名称'}
      ,{field: 'desc', title: '描述'}
      ,{field: 'status', width: 100, title:'状态', templet: '#buttonSwitch', minWidth: 80, align: 'center'}
      ,{title: '操作', width: 220, align: 'center', toolbar: '#back-role-list-operation'}
    ]]
  });
  

  //监听工具条
  table.on('edit(back-role-list)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
    admin.req({
      url: setter.api_back_role_update
      ,data: obj.data
      ,type: 'post'
      ,done: function(res){
        layer.msg(res.msg);
        layui.table.reload('back-role-list'); //重载表格
      }
    });
  });

  table.on('tool(back-role-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此角色？', function(index){
        admin.req({
          url: setter.api_back_role_delete
          ,data: {id:obj.data.id}
          ,type: 'post'
          ,done: function(res){
            layer.msg(res.msg);
            layui.table.reload('back-role-list'); //重载表格
          }
        });
        layer.close(index); //执行关闭 
      });
    }else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑角色'
        ,area: admin.screen() < 2 ? ['100%','430px'] : ['450px','430px']
        ,id: 'back-role-edit-popup'
        ,success: function(layero, index){
          view(this.id).render('role/form', data).done(function(){
            form.render(null, 'back-role-form');
            //监听提交
            form.on('submit(back-role-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              admin.req({
                url: setter.api_back_role_update
                ,data: field
                ,type: 'post'
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭
                  layui.table.reload('back-role-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }else if(obj.event === 'rolepermission'){
      admin.popup({
        title: '权限'
        ,area: admin.screen() < 2 ? ['100%','530px'] : ['450px','800px']
        ,id: 'back-role-permission-popup'
        ,maxmin: admin.screen() < 2 ? true:false
        ,skin: 'layui-layer-molv'
        ,success: function(layero, index){
          admin.req({
            url: setter.api_back_role_detail
            ,data: {id:data.id}
            ,async: false
            ,done: function(res){
              data = res.data
            }
          });
          $.each(data.permission, function (index, item) {
            data.permission[index]['lay_is_checked'] = item.has==1?true:false;
            data.permission[index]['lay_che_disabled'] = item.pid==0?true:false;
          });
          view(this.id).render('role/rolepermission', data).done(function(){
            treeGrid.render({
              id:'back-role-permission'
              ,elem: '#back-role-permission'
              ,data:data.permission
              ,cellMinWidth: 100
              ,idField:'id'//必須字段
              ,treeId:'id'//树形id字段名称
              ,treeUpId:'pid'//树形父id字段名称
              ,treeShowName:'name'//以树形式显示的字段
              ,height:580
              ,isFilter:false
              ,iconOpen:false
              ,isOpenDefault:true//节点默认是展开还是折叠【默认展开】
              ,loading:true
              ,isPage:false
              ,limit:999
              ,cols: [[
                  {type:'checkbox'}
                  ,{field:'name', title: '名称',}
              ]]
            });
            form.render(null, 'back-role-permission-form');
            //监听提交
            form.on('submit(back-role-permission-form-submit)', function(data){
              permission = treeGrid.checkStatus('back-role-permission');
              admin.req({
                url: setter.api_back_role_update
                ,data: {
                  id:data.field.id
                  ,permission:permission
                }
                ,type: 'post'
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭
                  layui.table.reload('back-role-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }
  });

  exports('role', {})
});